/**
 * 
 */
package com.maoye.ossp.sys.msg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.beust.jcommander.internal.Lists;
import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.example.entity.Student;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.msg.entity.TodoMessage;
import com.maoye.ossp.sys.msg.service.TodoMessageService;
import com.maoye.ossp.sys.msg.util.MessageConstant;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 
 * @author xiaohan
 * @date 2014年3月6日
 * @version
 * @filename com.maoye.ossp.sys.msg.MessageCont.java
 */

@Controller
@RequestMapping(value = "/sys/msg/")
public class MessageController extends BaseController {
	@Resource
	TodoMessageService todoMessageService;

	@SuppressWarnings({ "unchecked" })
	@RequestMapping("/list")
	public String getList(TodoMessage todoMessage, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, todoMessage);
		Page<TodoMessage> page = (Page<TodoMessage>) todoMessageService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/msg/list", model, request);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("view/{id}")
	public String view(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		TodoMessage todoMessage = todoMessageService.get(id);
		if (MessageConstant.TODO_TYPE_NOTIFY.equals(todoMessage
				.getMessageType())) {
			// 通知类待办，点击链接后待办消失
			List<String> targets = Lists.newArrayList();
			Long currentUserId = UserUtils.getCurrentUserId();
			targets.add(currentUserId == null ? "" : currentUserId.toString());
			todoMessageService.setPersonDone(id, targets);
		}
		String redirectUrl = todoMessage.getTitleLink();
		if (redirectUrl != null && redirectUrl.indexOf("/") != 0
				&& redirectUrl.indexOf("http") != 0) {
			redirectUrl = "/" + redirectUrl;
		}
		return "redirect:" + redirectUrl;
	}

	@RequestMapping("{id}/markRead")
	public String markRead(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		boolean isSuccess = false;
		try {
			isSuccess = todoMessageService.setPersonDone(id);
		} catch (Exception e) {
			addMessage(redirectAttributes, "操作失败！");
		}
		addMessage(redirectAttributes, isSuccess ? "成功！" : "失败！");
		return "redirect:/sys/msg/" + id + "/view";
	}

	@RequestMapping("{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		boolean isSuccess = false;
		try {
			isSuccess = todoMessageService.deleteTodo(id);
		} catch (Exception e) {
			addMessage(redirectAttributes, "操作失败！");
		}
		if (!isSuccess) {
			addMessage(redirectAttributes, "查询失败！");
		}
		return "redirect:/sys/msg/list";
	}

}
